package com.pro.controller;

import com.alibaba.fastjson.JSON;
import com.pro.pojo.JsLoginRecord;
import com.pro.pojo.JsUser;
import com.pro.pojo.JsonData;
import com.pro.service.JsLoginRecordService;
import com.pro.service.JsLoginUserService;
import com.pro.utils.IPAndAddressUtil;
import com.pro.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;

import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName:LoginUserController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2022/1/7 11:22
 * @Author: XJR
 */
@Controller
public class JsLoginUserController {

    @Autowired
    private JsLoginUserService service;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private JsLoginRecordService loginRecordService;

    @RequestMapping("/")
    public String tologin(){
        return "login";
    };

    /**
     * @author WYH
     * @date 2022/1/10 下午 8:09
     * @content : 获取account
     */
    private String account;

    @RequestMapping("/login/main")
    public String loginUser(HttpServletRequest request, HttpSession session){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        account = authentication.getName();
        JsLoginRecordController loginController=new JsLoginRecordController();
        JsLoginRecord loginRecord = new JsLoginRecord();
        UserDetails userDetails = userDetailsService.loadUserByUsername(account);
        JsUser jsUser = service.loginUser(account,userDetails.getPassword());
       if(jsUser!=null){
           session.setAttribute("user",jsUser);
           /*CYJ 获取登录信息*/
           loginRecord.setLoginState(2);
           loginRecord.setUserId(jsUser.getUserId());
           loginRecordService.addJsLoginRecord(loginRecord);
           return "index";
       }
        if(jsUser.getUserId()!=null){
            loginRecord.setLoginState(3);
            loginRecord.setUserId(jsUser.getUserId());
            loginRecordService.addJsLoginRecord(loginRecord);
        }
       return "login";
    }

    /**
     * @author WYH
     * @date 2022/1/11 下午 2:22
     * @content : 查询出用户登录信息返回给头部导航栏
     */
    @RequestMapping("/login/getLoginMsg")
    @ResponseBody
    public JsonData getLoginMsg() {
        List<JsUser> userList = service.getLoginMsg(account);
        JsUser userLast = userList.get(userList.size() - 1);
        JsUser userThis = null;
        if (userList.size()>=2){
            userThis = userList.get(userList.size() - 2);
        }else {
            userThis = userLast;
        }
        List<JsUser> jsUsers = new ArrayList<>();
        jsUsers.add(userLast);
        jsUsers.add(userThis);
        return jsUsers!=null?JsonData.buildSuccess(jsUsers):JsonData.buildError("服务端错误");
    }
    /**
     *
     * @author ZPT
     * @date 2022/1/16 16:26
     * @content :根据用户id查询信息
     */
    @RequestMapping("/login/getUserById")
    @ResponseBody
    public JsonData getUserById(Integer userId){
        JsUser user = service.getUserById(userId);
        return user!=null?JsonData.buildSuccess(user):JsonData.buildError("服务端错误");
    }
    /**
     *
     * @author ZPT
     * @date 2022/1/16 16:36
     * @content :修改密码，头像
     */
    @RequestMapping("/login/updateUser")
    @ResponseBody
    public JsonData updateUser(Integer userId,String password,String acatarUrl){
        Boolean updateBoolean = service.updateUser(userId, password, acatarUrl);
        return updateBoolean!=null?JsonData.buildSuccess(updateBoolean):JsonData.buildError("服务端错误");
    }


    /*验证码是否正确*/
    @RequestMapping("/login/isVerify")
    @ResponseBody
    public JsonData isVerify(HttpServletRequest request,String code){
        String verifyCode = (String) request.getSession().getAttribute("VerifyCode");
        if (code==null){
            return code!=null?JsonData.buildSuccess(false):JsonData.buildError("服务端错误");
        }else if (verifyCode.equals(code.toUpperCase())){
            return code!=null?JsonData.buildSuccess(true):JsonData.buildError("服务端错误");
        }else {
            return code!=null?JsonData.buildSuccess(false):JsonData.buildError("服务端错误");
        }
    }











}
